package com.dhnsoft.tank;

import com.dhnsoft.game.GameFrame;
import com.dhnsoft.game.LevelInfo;
import com.dhnsoft.util.Constant;
import com.dhnsoft.util.EnemyTanksPool;
import com.dhnsoft.util.MyUtil;

import java.awt.*;

/**
 * @Description
 * @ClassName EnemyTank
 * @Author dhn
 * @date 2020.11.18 21:06
 */
//敌人的坦克
public class EnemyTank extends Tank{

    private static Image[] enemyImg;
    private Color color;
    //记录5秒开始的时间
    private long aiTime;
    static {
        enemyImg = new Image[4];
        enemyImg[0]=MyUtil.createImage("images/lbU.png");
        enemyImg[1]=MyUtil.createImage("images/lbD.png");
        enemyImg[2]=MyUtil.createImage("images/lbL.png");
        enemyImg[3]=MyUtil.createImage("images/lbR.png");
    }

    public EnemyTank(){
        aiTime = System.currentTimeMillis();
        this.speed=DEFAULT_SPEED;
    }
    private EnemyTank(int x,int y, int dir){
        super(x,y,dir);
        aiTime = System.currentTimeMillis();
        this.speed=DEFAULT_SPEED;
    }

    @Override
    public void drawTank(Graphics g) {
        ai();
        g.drawImage(enemyImg[getDir()],getX()-RADIUS,getY()-RADIUS,null);

    }

    //用于创建一个敌人坦克
    public static Tank createEnemy(){
        int x = MyUtil.getRandomNumber(0,2) == 0 ? RADIUS
                : Constant.FRAME_WIDTH-RADIUS;
        int y = GameFrame.titleBarH+RADIUS;
        //Tank enemy = new EnemyTank(x, y, DIR_DOWN);
        int dir=DIR_DOWN;
        Tank enemy = EnemyTanksPool.get();
        enemy.setEnemy(true);
        //TODO
        enemy.setX(x);
        enemy.setY(y);
        enemy.setDir(dir);
        enemy.setEnemy(true);
        enemy.setState(STATE_MOVE);
        int maxHP = Tank.DEFAULT_HP*LevelInfo.getInstance().getLevelType();
        enemy.setHp(maxHP);
        enemy.setMaxHP(maxHP);
        enemy.setHp(Tank.DEFAULT_HP* LevelInfo.getInstance().getLevelType());
        return enemy;
    }
    /**
     * 敌人的AI
     * */
    private void ai(){
        if(System.currentTimeMillis()-aiTime>Constant.ENEMY_AI_INTERVAL){
            //间隔5秒随机一个状态
            setDir(MyUtil.getRandomNumber(DIR_UP,DIR_RIGHT+1));
            setState(MyUtil.getRandomNumber(0,2)==0?STATE_STAND:STATE_MOVE);
            aiTime=System.currentTimeMillis();

        }
        //比较小的概率去开火
        if(Math.random()<Constant.ENEMY_FIRE_PERCENT){
            fire();
        }
    }
}
